package cn.mymall.web.controller;

import cn.mymall.constant.MallConstant;
import cn.mymall.constant.OrderStatusConstant;
import cn.mymall.constant.PayConstant;
import cn.mymall.entity.Member;
import cn.mymall.entity.MemberAddressOrder;
import cn.mymall.entity.OrderItem;
import cn.mymall.entity.TradeOrder;
import cn.mymall.service.IMemberAddressOrderService;
import cn.mymall.service.IOrderItemService;
import cn.mymall.service.ITradeOrderService;
import cn.mymall.service.mall.ICartItemService;
import cn.mymall.vo.order.MallOrderSimpleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class HomeController extends BaseController{
    @Autowired
    private ITradeOrderService orderService;
    @Autowired
    private IOrderItemService orderItemService;
    @Autowired
    private IMemberAddressOrderService memberAddressOrderService;
    @RequestMapping("/home/index")
    public String home(ModelMap model) {
        return "front/home/index.ftl";
    }

    @RequestMapping("/home/order/list")
    public String order(Integer per, Integer page_no, String order_status, ModelMap model) {
        Map<String, Integer> filterPageInfo = filterPageInfo(page_no, per);
        page_no = filterPageInfo.get("page_no");
        per = filterPageInfo.get("per");
        Member member = getLoginMember();
        Page page = null;
        String express_status = order_status;
        if(StringUtils.isBlank(express_status)){
            page = getMallSubOrderList(member,per, page_no, null);
        }else if(StringUtils.equalsIgnoreCase(express_status, OrderStatusConstant.WAIT_SNED)){
            //待发货
            page = getMallSubOrderList(member,per, page_no, MallConstant.EXPRESSSTATUS_WAIT);
        }else if(StringUtils.equalsIgnoreCase(express_status,OrderStatusConstant.SNEDED)){
            //已发货
            page = getMallSubOrderList(member,per, page_no, MallConstant.EXPRESSSTATUS_SEND);
        }else if(StringUtils.equalsIgnoreCase(express_status,OrderStatusConstant.SUCCESS)){
            //已完成
            page = getMallSubOrderList(member,per, page_no, MallConstant.EXPRESSSTATUS_RECEIVE);
        }
        List<TradeOrder> tradeOrderList = page.getRecords();
        List<MallOrderSimpleVo> mallOrderSimpleVoList = mallSubOrderList2VoList(tradeOrderList);
        model.put("orderList", mallOrderSimpleVoList);
        return "front/home/all_order.ftl";
    }
    private Page<TradeOrder> getMallSubOrderList(Member member, Integer per, Integer page_no, String express_status) {
        QueryWrapper<TradeOrder> qw = new QueryWrapper();
        qw.eq("member_id", member.getId());
        qw.eq("pay_status", PayConstant.SUCCESS);
        if(StringUtils.isNotBlank(express_status)){
            qw.eq("express_status", express_status);
        }
        qw.orderByDesc("id");
        Page pageParams = new Page(page_no,per);
        Page<TradeOrder> page = orderService.page(pageParams, qw);
        return page;
    }

    private List<MallOrderSimpleVo> mallSubOrderList2VoList(List<TradeOrder> orderList) {
        List<MallOrderSimpleVo> voList = new ArrayList<>();
        for(TradeOrder order : orderList){
            MallOrderSimpleVo vo = new MallOrderSimpleVo(order);
            List<OrderItem>  orderItemList = orderItemService.list(new QueryWrapper<OrderItem>().eq("order_id", order.getId()));
            List<String> logoList = orderItemList.stream().map(OrderItem::getLogo).collect(Collectors.toList());
            vo.setLogo(logoList.get(0));
            vo.setProductNames(orderItemList.stream().map(OrderItem::getProductName).collect(Collectors.joining(",")));
            vo.setSkuNames(orderItemList.stream().map(OrderItem::getSkuName).collect(Collectors.joining(",")));
            vo.setSumQty(orderItemList.stream().mapToInt(OrderItem::getQty).sum());
            vo.setProductQty(orderItemList.stream().distinct().map(e->e.getProductId()).collect(Collectors.toList()).size());
            voList.add(vo);
        }
        return voList;
    }

    @RequestMapping("/home/order/{id}")
    public String order(@PathVariable int id, ModelMap model) {
        TradeOrder order = orderService.getById(id);
        MemberAddressOrder memberAddressOrder = memberAddressOrderService.getOne(new QueryWrapper<MemberAddressOrder>().eq("order_id", id));
        List<OrderItem>  orderItemList = orderItemService.list(new QueryWrapper<OrderItem>().eq("order_id", order.getId()));
        model.put("order", order);
        model.put("memberAddressOrder", memberAddressOrder);
        model.put("orderItemList", orderItemList);
        return "front/home/order.ftl";
    }
}
